package com.test.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.test.entity.FlowTemplateData;
import com.test.service.IFlowTemplateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;

/**
 * // 设置所有字段的相等条件，如果字段值为null，则根据null2IsNull参数决定是否设置为IS NULL
 * allEq(Map<String, Object> params)
 * allEq(Map<String, Object> params, boolean null2IsNull)
 * allEq(boolean condition, Map<String, Object> params, boolean null2IsNull)
 * -------
 * // 设置所有字段的相等条件，通过filter过滤器决定哪些字段应该被包含，如果字段值为null，则根据null2IsNull参数决定是否设置为IS NULL
 * allEq(BiPredicate<String, Object> filter, Map<String, Object> params)
 * allEq(BiPredicate<String, Object> filter, Map<String, Object> params, boolean null2IsNull)
 * allEq(boolean condition, BiPredicate<String, Object> filter, Map<String, Object> params, boolean null2IsNull)
 */
@Service
public class QueryAllEqService {

    @Autowired
    private IFlowTemplateDataService flowTemplateDataService;

    public void test01(){
        QueryWrapper<FlowTemplateData> queryWrapper = new QueryWrapper();
        Map map = new HashMap();
        map.put("tenant_id_",1);
        map.put("template_id_",2);
        queryWrapper.allEq(map);

        List<FlowTemplateData> list = flowTemplateDataService.list(queryWrapper);

        System.out.println("------------");
        System.out.println(list.size());
        System.out.println("------------");
    }

    /**
     * allEq(Map<String, Object> params, boolean null2IsNull)
     */
    public void test02(){
        Map map = new HashMap();
        map.put("tenant_id_",1);
        map.put("template_id_",2);
        map.put("template_name_",null);

        QueryWrapper<FlowTemplateData> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map, false);//不包含template_name_ is null
        List<FlowTemplateData> list = flowTemplateDataService.list(queryWrapper);

        QueryWrapper<FlowTemplateData> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.allEq(map, true);//包含template_name_ is null
        List<FlowTemplateData> list2 = flowTemplateDataService.list(queryWrapper2);

        System.out.println("------------");
        System.out.println(list.size());
        System.out.println(list2.size());
        System.out.println("------------");
    }

    /**
     * LambdaQueryWrapper
     */
    public void test03(){
        LambdaQueryWrapper<FlowTemplateData> queryWrapper = new LambdaQueryWrapper();
        Map<SFunction<FlowTemplateData,?>, Object> map = new HashMap();
        map.put(FlowTemplateData::getTenantId,1);
        map.put(FlowTemplateData::getTemplateId,2);
        queryWrapper.allEq(map);

        List<FlowTemplateData> list = flowTemplateDataService.list(queryWrapper);

        System.out.println("------------");
        System.out.println(list.size());
        System.out.println("------------");
    }

    /**
     * filter
     */
    public void test04(){
        Map map = new HashMap();
        map.put("tenant_id_",1);
        map.put("template_id_",2);
        map.put("template_name_",null);

        QueryWrapper queryWrapper = new QueryWrapper();
        //过滤出tenant的字段作为条件，其他条件忽略
        BiPredicate biPredicate = (field, value) -> field.toString().contains("tenant");
        queryWrapper.allEq(biPredicate, map, false);

        List<FlowTemplateData> list = flowTemplateDataService.list(queryWrapper);

        System.out.println("------------");
        System.out.println(list.size());
        System.out.println("------------");
    }
}
